﻿using DrCarDrive.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DrCarDrive.DBService
{
    class MedicineService
    {
        private DrcardriveDB db;

        public MedicineService()
        {
            db = new DrcardriveDB();
        }

        public int save(tbl_issue_medicine medicine)
        {
            DateTime currentDateTime = DateTime.Now;

            medicine.INSERT_DATE = currentDateTime;
            medicine.UPDATE_DATE = currentDateTime;

            if (medicine != null)
            {
                db.IssueMedicines.Add(medicine);
            }
            return db.SaveChanges();
        }

        internal int update(tbl_issue_medicine medicine)
        {
            DateTime currentDateTime = DateTime.Now;
            medicine.UPDATE_DATE = currentDateTime;

            if (medicine != null)
            {
                var original = db.IssueMedicines.SingleOrDefault(d => d.ID.Equals(medicine.ID));
                db.Entry(original).CurrentValues.SetValues(medicine);
            }
            return db.SaveChanges();
        }

        public List<tbl_issue_medicine> viewAll()
        {
            return db.IssueMedicines.ToList();
        }


        internal List<tbl_drive_medicine> getMedicineList()
        {
            return db.Medicines.ToList();
        }

        internal int saveMedicine(tbl_drive_medicine medicine)
        {
            DateTime currentDateTime = DateTime.Now;

            medicine.INSERT_DATE = currentDateTime;
            medicine.UPDATE_DATE = currentDateTime;

            if (medicine != null)
            {
                db.Medicines.Add(medicine);
            }
            return db.SaveChanges();
        }

        internal List<tbl_drive_medicine> viewMedicineList()
        {
            return db.Medicines.OrderBy(m => m.NAME).ToList();
        }

        internal bool inDatabase(tbl_drive_medicine medicine)
        {
            return db.Medicines.Any(m => m.NAME == medicine.NAME);
        }

        internal int updateMedicine(tbl_drive_medicine medicine)
        {
            DateTime currentDateTime = DateTime.Now;
            medicine.UPDATE_DATE = currentDateTime;

            if (medicine != null)
            {
                var original = db.Medicines.SingleOrDefault(m => m.ID.Equals(medicine.ID));
                db.Entry(original).CurrentValues.SetValues(medicine);
            }
            return db.SaveChanges();
        }

        internal int remove(tbl_drive_medicine medicine)
        {
            if (medicine != null)
            {
                tbl_drive_medicine original = (tbl_drive_medicine)db.Medicines.SingleOrDefault(d => d.ID.Equals(medicine.ID));
                db.Medicines.Remove(original);
            }
            return db.SaveChanges();
        }

        internal List<IssueMedicineView> getMedicineByPlaceID(string placeID)
        {
           // return db.IssueMedicines.Where(m => m.FKY_PLACE.Equals(placeID)).ToList();

            var query = (from i in db.IssueMedicines
                         where i.FKY_PLACE == placeID
                         join m in db.Medicines on i.FKY_MEDICINE equals m.ID into md
                         select new IssueMedicineView()
                         {
                             MedicineName = md.Select(mds => mds.NAME).FirstOrDefault(),
                             Dosage=i.DOSAGE,
                             Unit=i.UNIT,
                             Remark=i.REMARK
                         }).ToList();
            return query;
        }
    }
}
